import {createResourceApi, updateFileApi, getResourceListApi, deleteResourceApi, getOperateListApi} from "@/api/common.js";
export default{
	// 根据时间戳返回日期
	getDate(time){
		let date = new Date(time);
		let year = date.getFullYear();
		let month = (date.getMonth() + 1).toString().padStart(2, '0');
		let day = date.getDate().toString().padStart(2, '0');
		return `${year}-${month}-${day}`;
	},
	// 根据时间戳返回日期时间
	getTime(time){
		let date = new Date(time);
		let year = date.getFullYear();
		let month = (date.getMonth() + 1).toString().padStart(2, '0');
		let day = date.getDate().toString().padStart(2, '0');
		let hours = date.getHours().toString().padStart(2, '0');
		let minues = date.getMinutes().toString().padStart(2, '0');
		let seconds = date.getSeconds().toString().padStart(2, '0');
		return `${year}-${month}-${day} ${hours}:${minues}:${seconds}`;
	},
	//合同部分显示金额保留小数点后两位
	getAmount(amount){
		let money = (amount /100).toFixed(2);
		return money
	},
	//根据url判断是不是图片
	isImg(url){
		let imgArr = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff'];
		let file = url.split('.').pop();
		return imgArr.indexOf(file) == -1 ? false : true
	},
	//省市区字符串中获取省市
	getArea(area){
		let index = area.lastIndexOf(' ');
		let result = area.substring(0,index);
		return result;
	},
	// 审批类型列表
	getApprovalType(){
		const typeList = {
			'crm-project-carry-out-audit': {id: 1, key: 'crm-project-carry-out-audit', name: '项目实施申请'},
			'crm-project-deliver-audit': {id: 2, key: 'crm-project-deliver-audit', name: '项目申请交付'},
			'crm-project-pause-audit': {id: 3, key: 'crm-project-pause-audit', name: '项目申请暂停'},
			'crm-project-invalidate-audit': {id: 4, key: 'crm-project-invalidate-audit', name: '项目申请作废'},
			'crm-project-file-audit': {id: 5, key: 'crm-project-file-audit', name: '项目申请归档'},
			'crm-contract-audit': {id: 6, key: 'crm-contract-audit', name: '合同审批'},
			'crm-contract-invalidate-audit': {id: 7, key: 'crm-contract-invalidate-audit', name: '合同申请作废'},
			'crm-receivable-audit': {id: 8, key: 'crm-receivable-audit', name: '回款审批'},
			'crm-receivable-invalidate-audit': {id: 9, key: 'crm-receivable-invalidate-audit', name: '回款审批作废'},
		};
		return typeList;
	},
	// 获取资源列表
	async getResourceList(id,type){
		let data = {
			empty: false,
			resourceList: []
		}
		let res = await getResourceListApi({
			bizId: id,
			bizType: type
		}).catch(() => {
			data.empty = true;
			uni.hideLoading();
			return Promise.reject();
		})
		data.resourceList = [...res.data.list];
		if(res.data.total == 0){
			data.empty = true;
		}
		await uni.hideLoading();
		return data;
	},
	// 上传资源
	async uploade(id, type, url){
		let data = false;
		let res = await updateFileApi(url).catch(() => {
			return Promise.reject();
		})
		await uni.showLoading({
			mask: true,
			title: '加载中...'
		})
		await createResourceApi({
			bizId: id,
			bizType: type,
			url: res.data
		}).catch(() => {
			uni.hideLoading();
			return Promise.reject();
		})
		await uni.hideLoading();
		await uni.showToast({
			icon: 'success',
			title: '上传成功！'
		})
		data = true;
		return data;
	},
	// 删除资源
	async deleteFile(id){
		let data = false;
		await uni.showLoading({
			mask: true,
			title: '加载中...'
		})
		await deleteResourceApi(id).catch(() => {
			uni.hideLoading();
			return Promise.reject();
		})
		data = true;
		await uni.hideLoading();
		await uni.showToast({
			icon:"success",
			title:'删除成功！'
		})
		return data;
	},
	// 预览文件
	openFile(file){
		uni.downloadFile({
			url: file,
			success: (e) => {
				if (e.statusCode === 200){
					uni.openDocument({
						filePath: e.tempFilePath,
						showMenu: true,
						fail:(res)=>{
							console.log(res)
							uni.showToast({
								icon:"error",
								title:'文件不存在'
							})
						}
					})
				}else{
					uni.showToast({
						icon:"error",
						title:'操作失败'
					})
				}
			}
		})
	},
	// 获得操作记录
	async getOperateList(id, type){
		let data = {
			empty: false,
			list: []
		}
		let res = await getOperateListApi({
			bizId: id,
			type: type
		}).catch(() => {
			data.empty = true,
			uni.hideLoading();
			return Promise.reject();
		})
		data.list = [...res.data.list];
		if(res.data.total == 0){
			data.empty = true;
		}
		await uni.hideLoading();
		return data;
	},
}